using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

    public class RedDotTrigger : MonoBehaviour, IRedDotListener
    {
        public enum RedDotTriggerType
        {
            [InspectorName("无效果")] none,
            [InspectorName("显示GameObject")] active,
            [InspectorName("隐藏GameObject")] inActive,
        }
        
        public enum RedDotType
        {
            [InspectorName("任意")]
            any,
            [InspectorName("红点")]
            red,
            [InspectorName("New(新)")]
            news,
        }

        [Header("监听路径")] [SerializeField] public string treePath;

        [Header("监听类型")] [SerializeField] public RedDotType redDotType;

        [Header("触发效果")] [SerializeField] public RedDotTriggerType redDotTriggerType;
        
        [Header("GameObject")] [SerializeField] public GameObject targetGameObject;

        public Action<bool> onTrigger;

        private void Awake()
        {
            if(targetGameObject!=null)
                targetGameObject.SetActive(false);
        }
        
        private void OnEnable()
        {
            if (!string.IsNullOrEmpty(treePath))
            {
                RedDot.Register(this);
                UpdateView();
            }
        }

        private void OnDisable()
        {
            RedDot.UnRegister(this);
            if(targetGameObject!=null)
                targetGameObject.SetActive(false);
        }
        
        private void OnDestroy()
        {
            RedDot.UnRegister(this);
        }
        
        public void ReBind(string path, RedDotType redDotType, RedDotTriggerType redDotTriggerType)
        {
            treePath = path;
            this.redDotType = redDotType;
            this.redDotTriggerType = redDotTriggerType;
            
            if (string.IsNullOrEmpty(treePath))
            {
                RedDot.UnRegister(this);
                if(targetGameObject!=null)
                    targetGameObject.SetActive(false);
                return;
            }
            
            RedDot.Register(this);
            UpdateView();
        }

        public void UpdateView()
        {
            var count = 0;
            if (redDotType is RedDotType.news)
            {
                count = RedDot.Get(treePath, RedDot.RedDotType.news);
            }
            else if (redDotType is RedDotType.red)
            {
                count = RedDot.Get(treePath, RedDot.RedDotType.red);
            }
            else if (redDotType is RedDotType.any)
            {
                count = RedDot.Get(treePath, RedDot.RedDotType.news) + RedDot.Get(treePath, RedDot.RedDotType.red);
            }

            bool triggered = count > 0;
            
            if (redDotTriggerType is RedDotTriggerType.active && targetGameObject != null)
            {
                targetGameObject.SetActive(triggered);
            }
            else if (redDotTriggerType is RedDotTriggerType.inActive && targetGameObject != null)
            {
                targetGameObject.SetActive(!triggered);
            }
            
            onTrigger?.Invoke(count > 0);
        }

        public string GetListenPath()
        {
            return treePath;
        }

        int IRedDotListener.GetInstanceID()
        {
            return GetInstanceID();
        }
    }

